<template>
  <ych-modal
    :fullscreen="false"
    size="small"
    :title="title"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    :cancelText="$t('m.common.button.close')">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :label="$t('m.system.oauth2Manage.entity.clientSecretType')" :labelCol="labelCol"
                     :wrapperCol="wrapperCol">
          <a-radio-group buttonStyle="solid" v-decorator="['clientSecretType', validatorRules.clientSecretType]">
            <a-radio-button value="bcrypt">{{ $t('m.system.oauth2Manage.other.bcrypt') }}</a-radio-button>
            <a-radio-button value="noop">{{ $t('m.system.oauth2Manage.other.noop') }}</a-radio-button>
          </a-radio-group>
        </a-form-item>
        <a-form-item :label="$t('m.system.oauth2Manage.entity.clientSecret')" :labelCol="labelCol"
                     :wrapperCol="wrapperCol">
          <a-input v-decorator="['clientSecret', validatorRules.clientSecret]"
                   :placeholder="$t('m.system.oauth2Manage.verify.clientSecret')"></a-input>
        </a-form-item>
      </a-form>
    </a-spin>
  </ych-modal>
</template>

<script>

import {httpAction} from '@/api/manage'
import YchConstant from "@/api/YchConstant";

export default {
  name: "SysOauth2SecretModal",
  data() {
    return {
      form: this.$form.createForm(this),
      title: this.$t('m.system.oauth2Manage.other.changeSecret'),
      visible: false,
      model: {},
      labelCol: {
        xs: {span: 24},
        sm: {span: 6},
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 18},
      },
      confirmLoading: false,
      validatorRules: {
        clientSecret: {
          rules: [
            {required: true, message: this.$t('m.system.oauth2Manage.verify.clientSecret')},
          ]
        },
        clientSecretType: {
          initialValue: 'noop'
        },
      },
      url: {
        changeSecret: YchConstant.PREFIX_SYSTEM + "/oauth2/changeSecret",
      }
    }
  },
  methods: {
    open(record) {
      this.form.resetFields();
      this.model = {id: record.id}
      this.visible = true;
    },
    close() {
      this.$emit('close');
      this.visible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let formData = Object.assign(this.model, values);
          httpAction(this.url.changeSecret, formData, 'put').then((res) => {
            if (res.success) {
              that.messageUtil.success(res.message);
              that.$emit('ok');
            } else {
              that.messageUtil.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
            that.close();
          })
        }
      })
    },
    handleCancel() {
      this.close()
    },
  }
}
</script>
